var ctx = ctx;
var hrs=$("#hrsPossess").val();
var referral=$("#referral").val();
if(hrs=='1'){
    $("#hrsScore").attr("disabled", "true");
    $("#hrsReason").attr("disabled", "true");
}
if(referral=='1'){
    $("#referralCompany").attr("disabled", "true");
    $("#referralReason").attr("disabled", "true");
}
function initStatus(){
    var title=$("#gestationalTitle").val();
    var obj={};
    obj.id='gestationalTitle';
    obj.selectedIndex=parseInt(title)-1;
    selectOnclick(obj);
}
initStatus();
function selectOnclick(obj){
    if(obj.id=='hrsPossess'){
        if(obj.selectedIndex==0){
            $("#hrsScore").attr("disabled", "true");
            $("#hrsScore").val("");
            $("#hrsReason").attr("disabled", "true");
            //console.dir($("#s2id_hrsReason ul li"));
            //$("#s2id_hrsReason ul li").remove();
            /*问下自带下拉框怎么清空值*/
        }
        else{
            $("#hrsScore").removeAttr("disabled");
            $("#hrsReason").removeAttr("disabled");
        };
    }
    if(obj.id=='referral'){
        if(obj.selectedIndex==0){
            $("#referralCompany").attr("disabled", "true");
            $("#referralCompany").val("");
            $("#referralReason").attr("disabled", "true");
            $("#referralReason").val("");
        }
        else{
            $("#referralCompany").removeAttr("disabled");
            $("#referralReason").removeAttr("disabled");
        };
    }
    if(obj.id=='gestationalTitle'){
        if(obj.selectedIndex==0){
            changeStatus('screenProduction','enable');
            changeStatus('tangShai','disable');
            changeStatus('labLiverFunction','disable');
            changeStatus('labRenalFunction','disable');
            changeStatus('fetalMovement','disable');
            changeStatus('fetalHeartRateNst','disable');
            changeStatus('inPelvimetryCd','disable');
            changeStatus('inPelvimetryBd','disable');
            changeStatus('inPelvimetrySnwith','disable');
            changeStatus('thirtySeventhDoctor','disable');
        }else if(obj.selectedIndex==1){
            changeStatus('screenProduction','disable');
            changeStatus('tangShai','disable');
            changeStatus('labLiverFunction','disable');
            changeStatus('labRenalFunction','disable');
            changeStatus('fetalMovement','disable');
            changeStatus('fetalHeartRateNst','disable');
            changeStatus('inPelvimetryCd','disable');
            changeStatus('inPelvimetryBd','disable');
            changeStatus('inPelvimetrySnwith','disable');
            changeStatus('thirtySeventhDoctor','disable');
        }else if(obj.selectedIndex==2){
            changeStatus('screenProduction','disable');
            changeStatus('tangShai','enable');
            changeStatus('labLiverFunction','disable');
            changeStatus('labRenalFunction','disable');
            changeStatus('fetalMovement','disable');
            changeStatus('fetalHeartRateNst','disable');
            changeStatus('inPelvimetryCd','enable');
            changeStatus('inPelvimetryBd','enable');
            changeStatus('inPelvimetrySnwith','enable');
            changeStatus('thirtySeventhDoctor','disable');
        }else if(obj.selectedIndex==3){
            changeStatus('screenProduction','disable');
            changeStatus('tangShai','disable');
            changeStatus('labLiverFunction','enable');
            changeStatus('labRenalFunction','disable');
            changeStatus('fetalMovement','enable');
            changeStatus('fetalHeartRateNst','disable');
            changeStatus('inPelvimetryCd','enable');
            changeStatus('inPelvimetryBd','enable');
            changeStatus('inPelvimetrySnwith','enable');
            changeStatus('thirtySeventhDoctor','disable');
        }else if(obj.selectedIndex==4){
            changeStatus('screenProduction','disable');
            changeStatus('tangShai','disable');
            changeStatus('labLiverFunction','enable');
            changeStatus('labRenalFunction','disable');
            changeStatus('fetalMovement','enable');
            changeStatus('fetalHeartRateNst','disable');
            changeStatus('inPelvimetryCd','enable');
            changeStatus('inPelvimetryBd','enable');
            changeStatus('inPelvimetrySnwith','enable');
            changeStatus('thirtySeventhDoctor','disable');
        }else if(obj.selectedIndex==5){
            changeStatus('screenProduction','disable');
            changeStatus('tangShai','disable');
            changeStatus('labLiverFunction','enable');
            changeStatus('labRenalFunction','enable');
            changeStatus('fetalMovement','enable');
            changeStatus('fetalHeartRateNst','enable');
            changeStatus('inPelvimetryCd','disable');
            changeStatus('inPelvimetryBd','disable');
            changeStatus('inPelvimetrySnwith','disable');
            changeStatus('thirtySeventhDoctor','disable');
        }else if(obj.selectedIndex==6){
            changeStatus('screenProduction','disable');
            changeStatus('tangShai','disable');
            changeStatus('labLiverFunction','enable');
            changeStatus('labRenalFunction','enable');
            changeStatus('fetalMovement','enable');
            changeStatus('fetalHeartRateNst','enable');
            changeStatus('inPelvimetryCd','disable');
            changeStatus('inPelvimetryBd','disable');
            changeStatus('inPelvimetrySnwith','disable');
            changeStatus('thirtySeventhDoctor','enable');
        }else if(obj.selectedIndex==7){
            changeStatus('screenProduction','disable');
            changeStatus('tangShai','disable');
            changeStatus('labLiverFunction','enable');
            changeStatus('labRenalFunction','enable');
            changeStatus('fetalMovement','enable');
            changeStatus('fetalHeartRateNst','enable');
            changeStatus('inPelvimetryCd','disable');
            changeStatus('inPelvimetryBd','disable');
            changeStatus('inPelvimetrySnwith','disable');
            changeStatus('thirtySeventhDoctor','enable');
        }else if(obj.selectedIndex==8){
            changeStatus('screenProduction','disable');
            changeStatus('tangShai','disable');
            changeStatus('labLiverFunction','enable');
            changeStatus('labRenalFunction','enable');
            changeStatus('fetalMovement','enable');
            changeStatus('fetalHeartRateNst','enable');
            changeStatus('inPelvimetryCd','disable');
            changeStatus('inPelvimetryBd','disable');
            changeStatus('inPelvimetrySnwith','disable');
            changeStatus('thirtySeventhDoctor','enable');
        }else if(obj.selectedIndex==9){
            changeStatus('screenProduction','disable');
            changeStatus('tangShai','disable');
            changeStatus('labLiverFunction','enable');
            changeStatus('labRenalFunction','enable');
            changeStatus('fetalMovement','enable');
            changeStatus('fetalHeartRateNst','enable');
            changeStatus('inPelvimetryCd','disable');
            changeStatus('inPelvimetryBd','disable');
            changeStatus('inPelvimetrySnwith','disable');
            changeStatus('thirtySeventhDoctor','enable');
        }
        $.ajax({
            type: "GET",
            url: ctx+"/doctor/pregnantVisit/getVisitNum?clinicId="+$("#clinicId").val()+'&title='+$("#gestationalTitle").val(),
            dataType: "json",
            success: function (data) {
                var num=parseInt(data);
                if(parseInt($("#gestationalTitle").val())==parseInt($("#title").val())){
                    if($("#id").val()!=''&&$("#id").val()){
                        num=parseInt($("#tempNum").val());
                    }
                    else{
                        num++;
                    }
                }
                else{
                    num=num+1;
                }
                $("#visitInt").val(num);
            },
            error: function (data) {
                toastr.error('网络连接错误,请检查网络');
            }
        });
    }
}
function changeStatus(id,type){
    if(type=='disable'){
        $("#"+id).attr("disabled", "true");
        $("#"+id).val("");
    }
    else{
        $("#"+id).removeAttr("disabled");
    }
}
/*
 * 加载高危因素下拉框
 */
$("input[name='hrsReason']").select2({
    minimumInputLength: 1,
    multiple             : true,
    separator            : ";",
    initSelection        : function (element, callback) {   // 初始化时设置默认值
        $.ajax({
            url:ctx+"/doctor/pregnantRecords/automatic",
            data:{ids:element.val(),type:'HIGH_RISK_FACTOR'},
            type: 'post', // 提交方式 get/post
            success:function(data){
                var row = jQuery.parseJSON(data);
                callback(row.items)
            }
        })
    },
    createSearchChoice   : function(term, data) {           // 创建搜索结果（使用户可以输入匹配值以外的其它值）
        return { id: term, text: term };
    },
    ajax : {
        url      : ctx+"/doctor/pregnantRecords/automatic",              // 异步请求地址
        data: function (term) {
            return {
                q: term,type:'HIGH_RISK_FACTOR'
            };
        },
        results: function (data) {
            return {
                results: data.items
            };

        }
    },
    formatSelection : resultFormatSelection,  // 选择结果中的显示
    formatResult    : resultFormatResult,
    escapeMarkup : function (m) { return m; }

});
/*
 * 加载保健指导下拉框
 */
$("input[name='healthGuidance']").select2({
    minimumInputLength: 1,
    multiple             : true,
    separator            : ";",
    initSelection        : function (element, callback) {   // 初始化时设置默认值
        $.ajax({
            url:ctx+"/doctor/pregnantRecords/automatic",
            data:{ids:element.val(),type:'HEALTH_GUIDANCE'},
            type: 'post', // 提交方式 get/post
            success:function(data){
                var row = jQuery.parseJSON(data);
                callback(row.items)
            }
        })
    },
    createSearchChoice   : function(term, data) {           // 创建搜索结果（使用户可以输入匹配值以外的其它值）
        return { id: term, text: term };
    },
    ajax : {
        url      : ctx+"/doctor/pregnantRecords/automatic",              // 异步请求地址
        data: function (term) {
            return {
                q: term,type:'HEALTH_GUIDANCE'
            };
        },
        results: function (data) {
            return {
                results: data.items
            };

        }
    },
    formatSelection : resultFormatSelection,  // 选择结果中的显示
    formatResult    : resultFormatResult,
    escapeMarkup : function (m) { return m; }

});
function resultFormatResult(medata) {
    return medata.text;
}
function resultFormatSelection(medata) {
    return medata.text+"<input type='hidden' value='"+medata.id+"'/>";
}